package com.ruoyi.caigou.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.basedata.api.RemoteGoodsService;
import com.ruoyi.caigou.domain.dto.BuyAskDTO;
import com.ruoyi.caigou.domain.dto.BuyReqDTO;
import com.ruoyi.caigou.domain.dto.IdAndNumber;
import com.ruoyi.caigou.domain.pojo.BuyAskGoods;
import com.ruoyi.caigou.mapper.BuyAskGoodsMapper;
import com.ruoyi.caigou.service.IBuyReqService;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.basedata.Goods;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.caigou.mapper.BuyAskMapper;
import com.ruoyi.caigou.domain.BuyAsk;
import com.ruoyi.caigou.service.IBuyAskService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 采购询价Service业务层处理
 *
 * @author lvql
 * @date 2024-10-24
 */
@Slf4j
@Service
public class BuyAskServiceImpl implements IBuyAskService {
    @Autowired
    private BuyAskMapper buyAskMapper;

    @Autowired
    private BuyAskGoodsMapper buyAskGoodsMapper;

    @Autowired
    private RemoteGoodsService remoteGoodsService;

    @Autowired
    private IBuyReqService buyReqService;

    /**
     * 查询采购询价
     *
     * @param id 采购询价主键
     * @return 采购询价
     */
    @Override
    public BuyAsk selectBuyAskById(Integer id) {
        return buyAskMapper.selectBuyAskById(id);
    }

    /**
     * 查询采购询价列表
     *
     * @param buyAsk 采购询价
     * @return 采购询价
     */
    @Override
    public List<BuyAsk> selectBuyAskList(BuyAsk buyAsk) {
        return buyAskMapper.selectBuyAskList(buyAsk);
    }

    /**
     * 新增采购询价
     *
     * @param buyAsk 采购询价
     * @return 结果
     */
    @Override
    public int insertBuyAsk(BuyAsk buyAsk) {
        return buyAskMapper.insertBuyAsk(buyAsk);
    }

    /**
     * 修改采购询价
     *
     * @param buyAsk 采购询价
     * @return 结果
     */
    @Override
    public int updateBuyAsk(BuyAsk buyAsk) {
        return buyAskMapper.updateBuyAsk(buyAsk);
    }

    /**
     * 批量删除采购询价
     *
     * @param ids 需要删除的采购询价主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBuyAskByIds(Integer[] ids) {
        for (Integer id : ids) {
            BuyAsk buyAsk = buyAskMapper.selectBuyAskById(id);
            buyAskGoodsMapper.deleteBuyAskGoodsByBuyAsk(buyAsk.getCode());
        }
        return buyAskMapper.deleteBuyAskByIds(ids);
    }

    /**
     * 删除采购询价信息
     *
     * @param id 采购询价主键
     * @return 结果
     */
    @Override
    public int deleteBuyAskById(Integer id) {
        return buyAskMapper.deleteBuyAskById(id);
    }

    @Transactional
    @Override
    public int insertBuyAskPlus(BuyAskDTO buyAskDTO) {
        BuyAsk buyAsk = buyAskDTO.getBuyAsk();
        List<IdAndNumber> idAndNumberList = buyAskDTO.getIdAndNumberList();
        for (IdAndNumber idAndNumber : idAndNumberList) {
            buyAskGoodsMapper.insertBuyAskGoods(new BuyAskGoods(buyAsk.getCode(), idAndNumber.getId(), idAndNumber.getNumber()));
        }
        buyAsk.setCreateBy(SecurityUtils.getUsername());
        return buyAskMapper.insertBuyAsk(buyAsk);
    }

    @Override
    public BuyAskDTO selectBuyAskByIdPlus(Integer id) {
        BuyAsk buyAsk = selectBuyAskById(id);

        String code = buyAsk.getCode();
        String sourceCode = buyAsk.getSourceCode();
        if (sourceCode != null && sourceCode != "") {
            BuyReqDTO buyReqDTO = buyReqService.selectBuyReqByCodePlus(sourceCode);
        }
        List<BuyAskGoods> buyAskGoods = buyAskGoodsMapper.selectBuyAskGoodsByBuyAskCode(buyAsk.getCode());

        List<Integer> ids = new ArrayList<>();
        for (BuyAskGoods buyAskGood : buyAskGoods) {
            ids.add(buyAskGood.getGoodsId());
        }
        R<List<Goods>> goodsListByIds = R.ok();
        if (!ids.isEmpty()) {
            goodsListByIds = remoteGoodsService.getGoodsListByIds(ids, SecurityConstants.INNER);
        }
        if (R.FAIL == goodsListByIds.getCode()) {
            throw new ServiceException(goodsListByIds.getMsg());
        }
        log.info("selectBuyAskByIdPlus goodsListByIds result{}", goodsListByIds);
        BuyAskDTO buyAskDTO = new BuyAskDTO();
        buyAskDTO.setBuyAsk(buyAsk);
        buyAskDTO.setGoodsList(goodsListByIds.getData());

        List<IdAndNumber> idAndNumberList = new ArrayList<>();
        for (BuyAskGoods buyAskGood : buyAskGoods) {
            idAndNumberList.add(new IdAndNumber(buyAskGood.getGoodsId(), buyAskGood.getGoodsNumber()));
        }
        buyAskDTO.setIdAndNumberList(idAndNumberList);
        return buyAskDTO;
    }

    @Override
    public BuyAskDTO selectBuyAskByCode(String code) {
        BuyAsk buyAsk1 = new BuyAsk();
        buyAsk1.setCode(code);
        List<BuyAsk> buyAsks = buyAskMapper.selectBuyAskList(buyAsk1);
        if (buyAsks.isEmpty()) {
            throw new ServiceException("没有该询价单");
        }
        BuyAsk buyAsk = buyAsks.get(0);

        String sourceCode = buyAsk.getSourceCode();
        if (sourceCode != null && sourceCode != "") {
            BuyReqDTO buyReqDTO = buyReqService.selectBuyReqByCodePlus(sourceCode);
        }
        List<BuyAskGoods> buyAskGoods = buyAskGoodsMapper.selectBuyAskGoodsByBuyAskCode(buyAsk.getCode());

        List<Integer> ids = new ArrayList<>();
        for (BuyAskGoods buyAskGood : buyAskGoods) {
            ids.add(buyAskGood.getGoodsId());
        }
        R<List<Goods>> goodsListByIds = R.ok();
        if (!ids.isEmpty()) {
            goodsListByIds = remoteGoodsService.getGoodsListByIds(ids, SecurityConstants.INNER);
        }
        if (R.FAIL == goodsListByIds.getCode()) {
            throw new ServiceException(goodsListByIds.getMsg());
        }
        log.info("selectBuyAskByIdPlus goodsListByIds result{}", goodsListByIds);
        BuyAskDTO buyAskDTO = new BuyAskDTO();
        buyAskDTO.setBuyAsk(buyAsk);
        buyAskDTO.setGoodsList(goodsListByIds.getData());

        List<IdAndNumber> idAndNumberList = new ArrayList<>();
        for (BuyAskGoods buyAskGood : buyAskGoods) {
            idAndNumberList.add(new IdAndNumber(buyAskGood.getGoodsId(), buyAskGood.getGoodsNumber()));
        }
        buyAskDTO.setIdAndNumberList(idAndNumberList);
        return buyAskDTO;
    }
}
